package com.kzw.oa.exam.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.config.property.MyConfig;
import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.core.util.StringUtil;
import com.kzw.misc.utils.ContextUtil;
import com.kzw.oa.exam.model.Answer;
import com.kzw.oa.exam.model.ExamResult;
import com.kzw.oa.exam.model.Examine;
import com.kzw.oa.exam.model.ExamineItem;
import com.kzw.oa.exam.model.Question;
import com.kzw.oa.system.model.AppUser;
import com.kzw.oa.system.model.Dictionary;

/**
 * @author kzwang 2020-4-11 0:01:39
 */
@Service
@Transactional
public class QuestionService extends DefaultEntityManager<Question, Long> {
	
	@Autowired
	private ExamineService examService;
	@Autowired
	private AnswerService answerService;
	@Autowired
	private ExamResultService examResultService;
	@Autowired
	private MyConfig mycfg;


	/**
	 * 生成一份试卷和空的答案：
	 */
	@Transactional(readOnly = true)
	public List<Map<String, Object>> genQuestion(Examine exam) {
		
		// 1、保存考试的试题信息
		AppUser student = ContextUtil.getCurrentUser();
		ExamResult examResult = new ExamResult();
		examResult.setStudent(student);
		examResult.setExamine(exam);
		examResultService.saveOrUpdate(examResult);
		
		// 2、随机生成试题
		List<Map<String, Object>> data = new ArrayList<>();
		AppUser user = ContextUtil.getCurrentUser();
		long seed = Long.parseLong(user.getUsername());
		if("dev".equals(mycfg.getEnv())) { //测试环境
			seed = 100;
		}
		
		// 该试卷的题目
		Set<ExamineItem> items = exam.getItems();
		for(ExamineItem item : items) {
			String hql = "from Question where courseType.dicId=? and tag=?";
			List<Question> questions = findByHQL(true, hql, exam.getCourseType().getDicId(), item.getTag());
			
			// 打乱顺序
			Collections.shuffle(questions, new Random(seed));
			List<Question> list = questions.subList(0, item.getCnt());
			List<Long> ids = new ArrayList<>();
			for(Question q : list) {
				ids.add(q.getOid());
			}
			
			// 3、每一道题对应一个Answer
			Answer answer = new Answer();
			answer.setItem(item);
			answer.setResult(examResult);
			answer.setQuestionIds(StringUtils.join(ids, ","));
			answerService.saveOrUpdate(answer);
			
			Map<String, Object> map = new HashMap<>();
			map.put("subQList", list); 	// 该题目下的具体内容
			map.put("item", item);		// 试题信息
			data.add(map);
		}		
		
		return data;
	}
	
	/**
	 * 试题预览
	 * */
	@Transactional(readOnly = true)
	public List<Map<String, Object>> previewExamine(Examine exam) {
		
		// 2、随机生成试题
		List<Map<String, Object>> data = new ArrayList<>();
		long seed = 100;
		
		// 该试卷的题目
		Set<ExamineItem> items = exam.getItems();
		for(ExamineItem item : items) {
			String hql = "from Question where courseType.dicId=? and tag=?";
			List<Question> questions = findByHQL(true, hql, exam.getCourseType().getDicId(), item.getTag());
			
			// 打乱顺序
			Collections.shuffle(questions, new Random(seed));
			List<Question> list = questions.subList(0, item.getCnt());
			List<Long> ids = new ArrayList<>();
			for(Question q : list) {
				ids.add(q.getOid());
			}			
			
			Map<String, Object> map = new HashMap<>();
			map.put("subQList", list); 	// 该题目下的具体内容
			map.put("item", item);		// 试题信息
			data.add(map);
		}		
		
		return data;
	}

	/**
	 * 获得某次考试，我的题目
	 * */
	public List<Map<String, Object>> getMyQuestion(long examId) {
		Examine exam = examService.get(examId);
		AppUser student = ContextUtil.getCurrentUser();
		String hql = "from ExamResult where examine.oid=? and student=?";
		ExamResult examRet = findUniqueByHQL(hql, examId, student);
		List<Map<String, Object>> data = new ArrayList<>();
		
		// 如果没有数据，生成数据
		if(examRet == null) {
			data = genQuestion(exam);
		} else {
			// 该试卷的题目
			List<Answer> answers = examRet.getAnswers();
			for(Answer answer : answers) {
				String ids = answer.getQuestionIds();
				List<Question> list = getEntityDao().get(StringUtil.idStrToArr(ids, Long.class));
				
				Map<String, Object> map = new HashMap<>();
				map.put("subQList", list); 	// 该题目下的具体内容
				map.put("item", answer.getItem()); // 试题信息
				map.put("answer", answer);
				data.add(map);
			}
		}
		
		return data;
	}
	
	/**
	 * 获得某学生的试卷
	 * */
	public List<Map<String, Object>> getQuestion(ExamResult examRet) {
		List<Map<String, Object>> data = new ArrayList<>();
		
		// 该试卷的题目
		List<Answer> answers = examRet.getAnswers();
		for(Answer answer : answers) {
			String ids = answer.getQuestionIds();
			List<Question> list = getEntityDao().get(StringUtil.idStrToArr(ids, Long.class));
			
			Map<String, Object> map = new HashMap<>();
			map.put("subQList", list); 	// 该题目下的具体内容
			map.put("item", answer.getItem()); // 试题信息
			map.put("answer", answer);
			data.add(map);
		}
		
		return data;
	}
	
	
	/**
	 * 获得课程某题库的标签
	 * */
	@Transactional(readOnly = true)
	public List<String> findTags(Dictionary courseType) {
		String hql = "select distinct tag from Question where courseType.dicId=?";
		return findByHQL(true, hql,  courseType.getDicId());
	}
	
}
